import time
import hashlib


class BookSpiderMiddleware:

    def process_request(self, request, spider):
        # gen_proxy(request)
        orderno = "ZF202012202203B6gxPF"
        secret = "a6df16b19a0f4398af9c0e9c0f0987b3"
        request.meta['proxy'] = 'http://forward.xdaili.cn:80'
        timestamp = str(int(time.time()))  # timestamp
        string = "orderno=" + orderno + "," + "secret=" + secret + "," + "timestamp=" + timestamp
        md5_string = hashlib.md5(string.encode('utf-8')).hexdigest()  # sign
        sign = md5_string.upper()
        auth = "sign=" + sign + "&" + "orderno=" + orderno + "&" + "timestamp=" + timestamp
        request.headers["Proxy-Authorization"] = auth
        return None


def gen_proxy(request):
    orderno = "55ca5487b8c8462eb389cb8ee4b0c669"
    secret = "a6df16b19a0f4398af9c0e9c0f0987b3"
    request.meta['proxy'] = 'http://forward.xdaili.cn:80'
    timestamp = str(int(time.time()))  # timestamp
    string = "orderno=" + orderno + "," + "secret=" + secret + "," + "timestamp=" + timestamp
    md5_string = hashlib.md5(string.encode('utf-8')).hexdigest()  # sign
    sign = md5_string.upper()
    auth = "sign=" + sign + "&" + "orderno=" + orderno + "&" + "timestamp=" + timestamp
    request.headers["Proxy-Authorization"] = auth
